package com.hks360.car_treasure.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.hks360.car_treasure.R;


public class ToogleButtonView extends View implements View.OnClickListener{
	/**
	 *1.构造方法--实例化
	 *2.测量：onMeasure方法
	 *3.指定View的大小和位置：onLayout方法
	 *4.绘制视图：onDraw方法
	 *
	 * */
	private Bitmap backgroundBitmap;
	private Bitmap slideBitmap;
	private Paint paint;
	private boolean isOn = false;
	private float maxSlide ;
	private int maxLeft;
	private OnToogleListener listener;
	/*
	 * 点击事件是否生效，如果生效滑动事件无效
	 * true生效
	 * false无效
	 */
	private boolean isClicked = false;

	public ToogleButtonView(Context context) {
		super(context);
		initView();
	}

	private void initView() {
		paint = new Paint();
		paint.setAntiAlias(true);
		backgroundBitmap = BitmapFactory.decodeResource(getResources()
				, R.drawable.off);
		slideBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.on);
		maxRight = - backgroundBitmap.getWidth();
		maxSlide = maxRight;
		setOnClickListener(this);
	}

	public ToogleButtonView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
	}
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawBitmap(backgroundBitmap, 0, 0,paint);
		canvas.drawBitmap(slideBitmap, maxSlide, 0,paint);
	}
	private float startX;
	private float lastX;

	public boolean isOn() {
		return isOn;
	}

	private int maxRight;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = startX = event.getX();
			isClicked = true;
			break;

		case MotionEvent.ACTION_MOVE:
			float newX = event.getX();
			float dx = newX - startX;
			maxSlide = maxSlide + dx;
			float stance = newX -lastX;
			if(stance >112){
				flushView();
			}
			if(Math.abs(event.getX() - lastX)>5){
				isClicked = false;
			}
			startX = event.getX();
			break;

		case MotionEvent.ACTION_UP:
			if(!isClicked){
				float newX1 = event.getX();
				float dx1 = newX1 - lastX;
				if(dx1 > backgroundBitmap.getWidth()/2){
					isOn = true;
					maxSlide = 0;
				}else{
					isOn = false;
					maxSlide = maxRight;
				}
				invalidate();
				this.listener.onToogleChanged(isOn);
			}
			break;
		}
		return true;
	}
	public void onClick(View v){
		if(isClicked){
			flushStatus();
		}
		this.listener.onToogleChanged(isOn);
	}

	private void flushStatus() {
		isOn = !isOn;
		if(isOn){
			maxSlide = 0;
		}else{
			maxSlide = maxRight;
		}
		flushView();
	}

	private void flushView() {
			if(maxSlide > 0){
				maxSlide = 0;
			}
			if(maxSlide < maxRight){
				maxSlide = maxRight;
			}
			invalidate();

	}

	public void setOnToggleListener(OnToogleListener listener) {
		this.listener = listener;
	}

	public interface OnToogleListener {
		void onToogleChanged(boolean isOpen);
	}
}
